{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 人脸检测 API的开始"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "API_Key = \"nViWRP71ph0DWl2oEjlKEDn3R-X_2anN\"\n",
    "API_secret = \"ilB3hpzmGQB04tHFug5JcObKxDQIPEtK\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/detect\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi2.hdslb.com%2Fbfs%2Farchive%2Fa70ccd55ea07a7bac524de278e99174e50fab59c.jpg&refer=http%3A%2F%2Fi2.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649907143&t=a604e6bca4c8288c7e572ec230119cfe\"  \n",
    "}\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(url=face_detect,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647316207,bdfec32a-14c8-481f-86a1-e4ef52dadce0',\n",
       " 'time_used': 182,\n",
       " 'faces': [{'face_token': '3f1efc859dd28baee5b64802619ecb4c',\n",
       "   'face_rectangle': {'top': 114, 'left': 498, 'width': 118, 'height': 118}}],\n",
       " 'image_id': 'InQJg4ILqFYP8ofsEFUjTg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.人脸分析 face token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url \n",
    "Analyze_base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'nViWRP71ph0DWl2oEjlKEDn3R-X_2anN'\n",
    "API_sercret = 'ilB3hpzmGQB04tHFug5JcObKxDQIPEtK'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':'17bdb7a97796c163b387f4277c9647d0',\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 278,\n",
       " 'request_id': '1647316208,fb6e3823-a312-46fe-bfb3-99c7c22fc8be',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.095,\n",
       "     'neutral': 9.866,\n",
       "     'disgust': 7.849,\n",
       "     'anger': 0.357,\n",
       "     'surprise': 13.728,\n",
       "     'fear': 0.095,\n",
       "     'happiness': 68.01},\n",
       "    'beauty': {'female_score': 78.802, 'male_score': 78.707},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 22},\n",
       "    'mouthstatus': {'close': 0.0,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 100.0,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 3.434,\n",
       "     'stain': 0.911,\n",
       "     'acne': 1.564,\n",
       "     'health': 81.174},\n",
       "    'headpose': {'yaw_angle': 23.709248,\n",
       "     'pitch_angle': -10.661212,\n",
       "     'roll_angle': 7.5709553},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.105},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.105},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.105}},\n",
       "    'smile': {'threshold': 50.0, 'value': 60.546},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.185,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.003,\n",
       "      'no_glass_eye_open': 99.812,\n",
       "      'normal_glass_eye_close': 0.001,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.197,\n",
       "      'no_glass_eye_close': 0.031,\n",
       "      'occlusion': 0.206,\n",
       "      'no_glass_eye_open': 99.56,\n",
       "      'normal_glass_eye_close': 0.004,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 2.916},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.546,\n",
       "      'vector_z_component': 0.882,\n",
       "      'vector_x_component': -0.003,\n",
       "      'vector_y_component': 0.472,\n",
       "      'position_y_coordinate': 0.473},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.522,\n",
       "      'vector_z_component': 0.802,\n",
       "      'vector_x_component': 0.435,\n",
       "      'vector_y_component': 0.409,\n",
       "      'position_y_coordinate': 0.511}}},\n",
       "   'face_rectangle': {'width': 120, 'top': 83, 'left': 309, 'height': 120},\n",
       "   'face_token': '17bdb7a97796c163b387f4277c9647d0'}]}"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.人脸对比  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "API_key = 'nViWRP71ph0DWl2oEjlKEDn3R-X_2anN'\n",
    "API_sercret = 'ilB3hpzmGQB04tHFug5JcObKxDQIPEtK'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':'17bdb7a97796c163b387f4277c9647d0',\n",
    "    'face_token2':'bc4837c5504c0998dbe2c1d1ea53de63'\n",
    "}\n",
    "r = requests.post(base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647316209,9ed146fc-224b-4a5e-9cc3-6f54ac0e3176',\n",
       " 'time_used': 490,\n",
       " 'confidence': 76.561,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3 .封装函数应用（内部使用的接口）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'nViWRP71ph0DWl2oEjlKEDn3R-X_2anN'\n",
    "API_sercret = 'ilB3hpzmGQB04tHFug5JcObKxDQIPEtK'\n",
    "img_url_01 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'\n",
    "img_url_02 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647914417,e498869c-36c6-4cbe-b602-df93b1107bc7',\n",
       " 'time_used': 543,\n",
       " 'faces': [{'face_token': '88c59ae5d84c1d3f16a3bdfe47967361',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647914424,5914e0b4-d470-42da-a1d2-898617ee3f45',\n",
       " 'time_used': 319,\n",
       " 'faces': [{'face_token': '0d940a6341884eb1fcdfec7488e996bd',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
